import request from '@/request/request'
import axios from 'axios'

// 获取所有教师
export function getAllTeachers() {
  return request({
    url: '/api/admin/teacher/getAllTeachers',
    method: 'get'
  })
}

// 分页获取教师
export function getTeacherPage(pageNum = 1, pageSize = 10, teacherName = '', minLikes = null, maxLikes = null) {
  const params = { pageNum, pageSize }
  
  // 添加可选查询参数
  if (teacherName) {
    params.teacherName = teacherName
  }
  
  if (minLikes !== null) {
    params.minLikes = minLikes
  }
  
  if (maxLikes !== null) {
    params.maxLikes = maxLikes
  }
  
  return request({
    url: '/api/admin/teacher/getTeacherPage',
    method: 'get',
    params
  })
}

// 根据ID获取教师
export function getTeacherById(id) {
  return request({
    url: '/api/admin/teacher/getTeacherById',
    method: 'get',
    params: { id }
  })
}

// 添加教师
export function addTeacher(data) {
  return request({
    url: '/api/admin/teacher/createTeacher',
    method: 'post',
    data
  })
}

// 修改教师
export function updateTeacher(id, data) {
  // Set the ID in the data object if not already present
  if (data.id === null || data.id === undefined) {
    data.id = id
  }
  return request({
    url: '/api/admin/teacher/updateTeacher',
    method: 'post',
    data
  })
}

// 删除教师
export function deleteTeacher(id) {
  return request({
    url: '/api/admin/teacher/deleteTeacher',
    method: 'delete',
    params: { id }
  })
}

// 上传讲师头像
export function uploadTeacherAvatar(file) {
  const formData = new FormData()
  formData.append('file', file)
  
  // 使用axios直接发送请求，避开request拦截器对Content-Type的修改
  return axios({
    url: 'http://localhost:8080/api/admin/teacher/uploadTeacherAvatar',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  }).then(response => {
    return response.data;
  }).catch(error => {
    console.error('上传头像失败:', error);
    return Promise.reject(error);
  });
}

// 获取教师选择列表
export function getTeacherSelectList() {
  return request({
    url: '/api/teachers/admin/TeacherSelectList',
    method: 'get'
  })
}